from db.utils import Mysql
import os
import time
dir_path = os.path.dirname(__file__)
folder_path = os.path.join(dir_path,'images')
count = 0
# 连接数据库并创建数据表
mysql_instance = Mysql(host='127.0.0.1',user='root',password='',db='mydb')
mysql_instance.create_table()

mysql_instance.delete_table()
# 遍历读取图片二进制数据并写入数据库
startTime = time.time()
for img_name in os.listdir(folder_path):
    img_url = os.path.join(folder_path,img_name)
    with open(img_url,'rb') as f:
        img_data = f.read()
        f.close()
        mysql_instance.insert_table(img_name.split('.')[0],img_data)
        count +=1


# 调用函数开始爬取

endTime = time.time()
runTime = endTime - startTime
print(f'用时{runTime}秒',f'共{count}张图片')

